#region

using System;
using System.Linq;
using System.Threading;
using System.Web.Mvc;
using log4net;
using xConnected.Common.CategoryNew;
using xConnected.Core.Model;
using xConnected.Service;

#endregion

namespace xConnected.Controllers
{
    public class CategoryController : BaseController<Category>
    {
        private readonly CategoryService _categoryService;

        public CategoryController(CategoryService categoryService)
        {
            _categoryService = categoryService;
        }

        public JsonResult List(string search = "", int pageIndex = 0, int pageSize = 10)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var page = search != ""
                               ? _categoryService.GetAllPagedSearched(search, pageIndex, pageSize)
                               : _categoryService.GetAllPaged(pageIndex, pageSize);
                var categories = page.Entities.ToList().Select(e => new
                                                                        {
                                                                            e.Id,
                                                                            e.Created,
                                                                            e.Updated,
                                                                            Description =
                                                                        CategoryNew.ResourceManager.GetString(e.Key,
                                                                                                              Thread.
                                                                                                                  CurrentThread
                                                                                                                  .
                                                                                                                  CurrentCulture),
                                                                            e.Key
                                                                        });

                return
                    Json(
                        new
                            {
                                search,
                                page.Count,
                                page.CurrentPage,
                                page.PageSize,
                                page.PagesCount,
                                Entities = categories
                            }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new {Result = "ERROR", ex.Message}, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public JsonResult Save(Category category)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                _categoryService.SaveOrUpdate(category);
                return Json(category.Id, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new {Result = "ERROR", ex.Message}, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public JsonResult DeleteCategory(int id)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var category = _categoryService.GetById(id);
                _categoryService.Delete(category);
                return Json("OK", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new {Result = "ERROR", ex.Message}, JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetCategoriesAutocomplete(string searchTerm, int maxRows)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var categories =
                    _categoryService.GetAll().ToList().OrderBy(
                        c => CategoryNew.ResourceManager.GetString(c.Key, Thread.CurrentThread.CurrentCulture)).Select(
                            c => new
                                     {
                                         Description =
                                     CategoryNew.ResourceManager.GetString(c.Key, Thread.CurrentThread.CurrentCulture),
                                         c.Id
                                     });
                if (searchTerm != "")
                {
                    categories =
                        categories.Where(l => l.Description.ToLower().Contains(searchTerm.ToLower())).Take(maxRows);
                }
                return Json(categories, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new {Result = "ERROR", ex.Message}, JsonRequestBehavior.AllowGet);
            }
        }
    }
}